class Module_ {
    String name;
    int port = 0;
}

class Host_ {
    String host;
    int port = 22;
    String username = "root";
    String password = "wsdc1993";
    //  暴露的 dubbo的地址 使得可以访问到这个地址 (本身访问)
    String dubboHost = "172.17.0.1";
    //  暴露服务的注册地址 (本身访问)
    String discoveryHost = "192.168.31.145";
}

def a = 0;

//  定义项目地址
def root = "/ksv";
def workspace = 'workspace';
def project = "manager";
def remote = 'https://gitee.com/wsdchigh/manager.git';
def branch = 'dev';
def version = "v1";
def port = 6300;
//  定义模块
def modules = [
    :
];
//  docker仓库远程地址    推送数据
def remoteDockerRegistry = "wsdchigh:5000";

//  需要事先指定好 java maven gradle 环境变量

def workPath = root + "/" + workspace + "/" + project;

//  需要发布的机器列表
def hostList = [

];

Host_ host0 = new Host_();
host0.host = "192.168.31.145"
hostList.add(host0);


def deploy = {  ->
    sh "cd ${workPath}/app/build/libs;" +
            " docker build -f ${workPath}/Dockerfile -t ${project}:${version} . ;" +
            " docker tag ${project}:${version} ${remoteDockerRegistry}/${project}:${version} ;" +
            " docker push ${remoteDockerRegistry}/${project}:${version} ;"

    //  启动docker镜像
    hostList.each({ host ->
        //def dubboPort = k.port + 1;
        def service = sh returnStdout: true, script: "docker ps -a | grep ${project}: | awk '{print \$1} '";
        if(service != null && !service.trim().equals("")){
            sh " docker stop  ${project} ; docker rm ${project} "
        }


        def dubboHost_ = host.dubboHost;
        sh "sshpass -p ${host.password} ssh -P${host.port} ${host.username}@${host.host} " +
                " docker pull ${remoteDockerRegistry}/${project}:${version} ;" +
                " docker run -d --name ${project} " +
                //  ip映射
                " --add-host=namesrv.v2:172.17.0.1 --add-host=broker.a.ip1:172.17.0.1" +
                " --add-host=broker.a.ip2:172.17.0.1 --add-host=broker.as.ip1:172.17.0.1 " +
                " --add-host=broker.as.ip2:172.17.0.1 --add-host=broker.b.ip1:172.17.0.1 " +
                " --add-host=broker.b.ip2:172.17.0.1 --add-host=broker.bs.ip1:172.17.0.1 " +
                " --add-host=broker.bs.ip2:172.17.0.1 " +
                " --add-host=mongo.wsdchigh.top:192.168.31.145 " +
                " --add-host=nacos.wsdchigh.top:172.17.0.1 --add-host=proxy.wsdchigh.top:172.17.0.1 " +
                " --add-host=redis.wsdchigh.top:172.17.0.1 --add-host=mysql.wsdchigh.top:172.17.0.1 " +
                " --add-host=dubbo.wsdchigh.top:${dubboHost_} --add-host=discovery.wsdchigh.top:${host.discoveryHost}" +
                " --net=host "+
                //" -p ${port}-${port+9}:${port}-${port+9} " +
                " ${remoteDockerRegistry}/${project}:${version} "
    })
};

def gitChange = false;
pipeline {
    agent any

    triggers {
        cron('H/5 * * * *')
    }

    stages {
        stage('项目准备') {
            steps {
                script {
                    if (true) {
                        //  如果不存在目录则创建目录
                        sh "if [ ! -d ${workPath} ] ; then mkdir -p ${workPath} ; fi; "
                        //  检测git是否存在 不存在则创建
                        def git_ = sh returnStdout: true, script: " cd ${workPath} ; ls -a | grep '.git' | awk '{print \$1}'";
                        echo git_+"1111111"
                        //  如果存在git项目 就不管了
                        if (git_ != null && git_.contains(".git")) {
                            echo "git没有更新代码"
                            gitChange = false;
                        } else {
                            gitChange = true;
                            //  初始化项目
                            sh "cd ${workPath} ; " +
                                    " git init ;" +
                                    " git remote add origin ${remote} ;" +
                                    " git fetch ;" +
                                    " git checkout ${branch} ;" +
                                    " gradle init ;" +
                                    " gradle build -x test ;"
                        }

                        //  生成时间戳   去掉换行符
                        version = sh returnStdout: true, script: "date +%N | awk '{print \$1}'";
                        version = version.trim();
                    }
                }
            }
        }

        stage('更新代码') {
            steps {
                script {
                    //  版本判断C
                    def originVersion = sh returnStdout: true, script: "cd ${workPath};git reflog | awk '{print \$1}'"
                    sh "cd ${workPath};git pull origin ${branch}"
                    def nowVersion = sh returnStdout: true, script: "cd ${workPath};git reflog | awk '{print \$1}'"

                    echo "之前版本号 ${originVersion} 现在版本号 ${nowVersion}"

                    //  如果版本相同则不再编译 否则需要编译
                    if (originVersion.equals(nowVersion)) {
                        //  相同则不要进行编译
                        gitChange = false;
                    } else {
                        sh "cd ${workPath} ; gradle build -x test";
                        gitChange = true
                    }
                }
            }
        }

        //  针对具体的服务进行分发
        stage("数据分发"){
            when {
                expression {
                    return gitChange;
                }
            }
            steps {
                script {
                    deploy();
                }
            }
        }
    }
}